package com.wufeng.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wufeng.dto.R;
import com.wufeng.entity.ArticleLikeRecord;
import com.wufeng.service.ArticleLikeRecordService;
import com.wufeng.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  文章点赞记录表(ArticleLikeRecord)表控制层
 */
@RestController
@RequestMapping("/articlelikerecord")
public class ArticleLikeRecordController {

    @Autowired
    private ArticleLikeRecordService articleLikeRecordService;

    @Autowired
    private ArticleService articleService;

    /**
     * 根据用户id和文章id添加或更新记录
     *
     * @param articleLikeRecord
     * @return
     */
    @Transactional
    @PostMapping("/addOrUpdateArticleLikeRecordByUserIdAndArticleId")
    public R addOrUpdateArticleLikeRecordByUserIdAndArticleId(@RequestBody ArticleLikeRecord articleLikeRecord) {

        ArticleLikeRecord record = articleLikeRecordService.getOne(
                new QueryWrapper<ArticleLikeRecord>().
                        eq("userId", articleLikeRecord.getUserid()).
                        eq("articleId", articleLikeRecord.getArticleid())
        );
        Map<String, Object> map = new HashMap<>();
        boolean isUpdate = false;
        if (record == null) {//添加
            articleLikeRecord.setIslike(1);//点赞

            isUpdate = articleLikeRecordService.save(articleLikeRecord);
            //更新文章点赞数+1
            articleService.incrementLikeCount(articleLikeRecord.getArticleid());

            map.put("isUpdate", isUpdate);
            return R.ok(map);
        }

        //更新
        if (record.getIslike() == 1) {
            record.setIslike(0);
            //更新文章点赞数-1
            articleService.decrementLikeCount(articleLikeRecord.getArticleid());
        } else {
            record.setIslike(1);
            //更新文章点赞数+1
            articleService.incrementLikeCount(articleLikeRecord.getArticleid());
        }
        isUpdate = articleLikeRecordService.update(record, new QueryWrapper<ArticleLikeRecord>().
                eq("userId", articleLikeRecord.getUserid()).
                eq("articleId", articleLikeRecord.getArticleid())
        );
        map.put("isUpdate", isUpdate);
        return R.ok(map);
    }


    /**
     * 根据文章id获取该文章的点赞数
     *
     * @param articleId
     * @return
     */
    @GetMapping("/getLikeCountByArticleId")
    public R getLikeCountByArticleId(@RequestParam("articleId") Integer articleId) {
        long count = articleLikeRecordService.count(new QueryWrapper<ArticleLikeRecord>()
                .eq("articleId", articleId)
                .eq("isLike", 1));
        HashMap<String, Object> map = new HashMap<>();
        map.put("likeCount", count);
        return R.ok(map);
    }

    /**
     * 根据文章id和用户id判断是否点赞
     * @param articleLikeRecord
     * @return
     */
    @PostMapping("/getIsLikeByUserIdAndArticleId")
    public R getIsLikeByUserIdAndArticleId(@RequestBody ArticleLikeRecord articleLikeRecord) {
        ArticleLikeRecord record = articleLikeRecordService.getOne(
                new QueryWrapper<ArticleLikeRecord>().
                        eq("userId", articleLikeRecord.getUserid()).
                        eq("articleId", articleLikeRecord.getArticleid()).
                        eq("isLike", 1)
        );
        boolean isLike = false;
        if (record != null) {
            isLike = true;
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("isLike", isLike);
        return R.ok(map);
    }

    /**
     * 获取用户点赞的文章
     * @param userId
     * @return
     */
//    @GetMapping("/getArticleLikeRecordByUserId")
//    public R getArticleLikeRecordByUserId(@RequestParam("userId") Integer userId) {
//        List<ArticleLikeRecord> list = articleLikeRecordService.list(
//                new QueryWrapper<ArticleLikeRecord>().eq("userId", userId)
//        );
//        return R.ok(list);
//
//    }
}
